package com.yz.crm.home.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yz.crm.passport.model.SessionUser;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * */
@Controller
@RequestMapping
public class HomeAction {

	@RequestMapping("/index")
	public String index(SessionUser user){
		
		/*if (user==null){
			return "redirect:/passport/login";
		}
		
		if (user.isAdmin()){
			return "admin/index";
		}*/
		
		return  "test/upload";
	}

	@RequestMapping("/upload")
	@ResponseBody
	public Map upload(
			HttpServletRequest request,
			@RequestParam(value = "identity", required = false) MultipartFile identity,
			@RequestParam(value = "video", required = false) MultipartFile video,
			@RequestParam(value = "changeImg", required = false) MultipartFile changeImg
	){

		String path =generateOrderFiles("11",changeImg);
		//String path =generateOrderFiles("11",identity);
		//String videoPaht =generateOrderFiles("22",video);
		System.out.println(path);
		//System.out.println(videoPaht);
		Map<String ,String > resultMap = new HashMap<>();
		resultMap.put("path",path);
		return  resultMap;
	}


	// 生成订单附件
	private String generateOrderFiles(String orderId, MultipartFile multipartFile) {
		String filePath ="";
		try {
			if (multipartFile == null || multipartFile.getSize() == 0) {
				return null;
			}
			String resPath = "d:/test";
			String orderPath = "order/";

			File file = null;
			String fileName = multipartFile.getOriginalFilename();


			String newFileName =orderPath+fileName;
			filePath = filePath + "/" + newFileName;

			// 生成目录
			file = new File(resPath+"/"+orderPath);
			if (!file.exists()) {
				file.mkdirs();
			}
			// 生成文件
			file = new File(resPath, filePath);
			//转成数据
			byte[] datas = multipartFile.getBytes();
			multipartFile.transferTo(file);
		return  filePath;
		} catch (Exception ex) {

		}
		return filePath;
	}


}
